package cn.ds.fly.modules.sys.interceptor;

import cn.ds.fly.utils.SystemContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 处理分页<br>
 * 项目名称:fly-admin<br>
 * 包名:cn.ds.fly.modules.sys.interceptor<br>
 * 类名:FlyPageInterceptor<br>
 * 用于:fly-admin<br>
 * 创建时间:2018年01月25日<br>
 * 更新时间:2018年01月25日<br>
 *
 * @author :lds（创建人）<br>
 * @version :v1.0（版本号）<br>
 * @since jdk1.8
 */
public class FlyPageInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        try {
            SystemContext.setCurrentPage(StringUtils.isBlank(currentPage)?1:Integer.valueOf(currentPage));
            SystemContext.setPageSize(StringUtils.isBlank(pageSize)?10:Integer.valueOf(pageSize));
        }catch (Exception e){
            SystemContext.setCurrentPage(1);
            SystemContext.setPageSize(10);
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
        SystemContext.removeCurrentPage();
        SystemContext.removePageSize();
    }
}
